package com.jeesite.modules.exam.score.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.exam.notice.entity.DcSupExamNotice;
import com.jeesite.modules.exam.notice.service.DcSupExamNoticeService;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.score.entity.DcSupExamScore;
import com.jeesite.modules.exam.score.service.DcSupExamScoreService;
import com.jeesite.modules.exam.score.vo.DcSupExamVo;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 考核自评
 */
@Controller
@RequestMapping("${adminPath}/score/dcSupExamZpScore")
@RequiredArgsConstructor
public class DcSupExamZpScoreController extends BaseController {

    private final DcSupExamNoticeService dcSupExamNoticeService;

    private final DcSupExamScoreService dcSupExamScoreService;

    private final SysOfficeUnitService  sysOfficeUnitService;

    /**
     * 自评页面
     */
    @RequiresPermissions("score:dcSupZpScore:view")
    @RequestMapping("view")
    public String indexView(DcSupExamNotice notice, Model model) {
        DcSupExamNotice dcSupExamNotice = dcSupExamNoticeService.get(notice.getId());
        List<DcSupExamSumQuotaInfo> zpQuotaScoreList = dcSupExamScoreService.findZpListData(dcSupExamNotice);
        DcSupExamScore dcSupExamScoreVo = new DcSupExamScore();
        dcSupExamScoreVo.setExamId(notice.getExamId());
        dcSupExamScoreVo.setNoticeId(notice.getId());
        dcSupExamScoreVo.setScoreType("1");
        dcSupExamScoreVo.setExameOrg(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
        DcSupExamScore dcSupExamScore = dcSupExamScoreService.findDcSupExamScore(dcSupExamScoreVo);//获取自评信息
        DcSupExamVo dcSupExamVo = new DcSupExamVo();
        dcSupExamVo.setDcSupExamNotice(dcSupExamNotice);
        dcSupExamVo.setDcSupScoreList(zpQuotaScoreList);
        dcSupExamVo.setDcSupExamScore(Optional.ofNullable(dcSupExamScore).orElseGet(DcSupExamScore::new));
        model.addAttribute("dcSupExamZp", dcSupExamVo);
        return "modules/exam/score/dcSupExamZpScore";
    }


    /**
     * 保存自评分数
     */
    @RequiresPermissions("score:dcSupZpScore:view")
    @PostMapping("/saveScore")
    @ResponseBody
    public String saveScore(DcSupExamVo dcSupExamVo) {
        if (UserUtils.getUser().getUserCode().equals("system")) {
            return renderResult(Global.FALSE,"操作禁止！");
        }
        dcSupExamScoreService.saveZpScore(dcSupExamVo);
        return renderResult(Global.TRUE, "保存成功！");
    }

    /**
     * 提交自评
     */
    @RequiresPermissions("score:dcSupZpScore:view")
    @PostMapping("/subScore")
    @ResponseBody
    public String subScore(DcSupExamVo dcSupExamVo) {
        if (UserUtils.getUser().getUserCode().equals("system")) {
            return renderResult(Global.FALSE,"操作禁止！");
        }
        dcSupExamScoreService.subZpScore(dcSupExamVo);
        return renderResult(Global.TRUE, "提交成功！");
    }
}
